package com.settlement.system.controller;

import com.settlement.system.common.enums.ReturnStatusEnum;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.common.util.common.FasUtil;
import com.settlement.system.common.util.common.RedisUtil;
import com.settlement.system.common.util.common.StrUtil;
import com.settlement.system.model.entity.common.BasePager;
import com.settlement.system.model.entity.common.SinglePage;
import com.settlement.system.model.entity.common.TblCostWeightRecord;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblCostWeightRecordService;
import com.settlement.system.service.ITblCostWeightSummaryService;
import com.settlement.system.service.ITblRrChannelService;
import io.swagger.annotations.Api;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【RR-费用分配权数】菜单 的 【操作记录】页签 前端控制器
 *
 * @author xiaochuan
 * @since 2022-12-28
 */

@Api(tags = "【RR-费用分配权数】菜单 的 【操作记录】页签", description = "【RR-费用分配权数】菜单 的 【操作记录】页签")
@RestController
@RequestMapping("/tblCostWeightRecord")
public class TblCostWeightRecordController extends BaseController<TblCostWeightRecord> {

    @Resource
    private ITblCostWeightRecordService tblCostWeightRecordService;

    @Resource
    private ITblCostWeightSummaryService tblCostWeightSummaryService;

    @Resource
    private ITblRrChannelService tblRrChannelService;

    @Override
    public IBaseService init() {
        return tblCostWeightRecordService;
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        String channelNo = (String) model.get("channelNo");
        if (org.apache.commons.lang3.StringUtils.isNotEmpty(channelNo)) {
            List<String> channelNoList = Arrays.asList(channelNo.split(","));
            model.put("channelNoList", channelNoList);
        }
        FasUtil.formatInQueryCondition(model, "channelNo", "accountingPeriod");
        super.setParamsModel(model);
    }

    @Override
    protected ObjectDataResponse insert(@RequestBody TblCostWeightRecord model, HttpServletRequest request) throws ServiceException {
        boolean ifCanRun = RedisUtil.checkBeforeStart(TblCostWeightRecordController.class.getName());
        if (!ifCanRun) {
            return new ObjectDataResponse(ReturnStatusEnum.CHECK_FAILURE.getValue(), "已有操作进行中，请稍后再试");
        }

        Map<String, Object> params = new HashMap<>();
        params.put("", "");
        FasUtil.formatInQueryCondition(params, "channelNo");
        int existsCount = tblCostWeightSummaryService.selectCount(params);
        if (existsCount > 0) {
            return new ObjectDataResponse(ReturnStatusEnum.CHECK_FAILURE.getValue(), "已存在数据，不允许 重复获取数据；");
        }

        String millis = "-" + System.currentTimeMillis();
        setDefualt(model);
        model.setUpdateUser(millis);
        tblCostWeightRecordService.insert(model);

        return new ObjectDataResponse(200, "新增成功");
        //return super.insert(model, request);
    }

    private void formatData(List<TblCostWeightRecord> datas) {
        if (!CollectionUtils.isEmpty(datas)) {
            for (TblCostWeightRecord detail : datas) {
                String channelNo = detail.getChannelNo();
                if (StringUtils.isNotEmpty(channelNo)) {
                    String[] split = channelNo.split(",");
                    List<String> collect = Arrays.asList(channelNo.split(","))
                            .stream().map(s -> tblRrChannelService.getNameByNo(s))
                            .collect(Collectors.toList());
                    String channelName = StrUtil.toString(collect.toArray());
                    detail.setChannelName(channelName);
                }
            }
        }
    }

    @Override
    protected ObjectDataResponse getPageList(HttpServletRequest request, @RequestBody BasePager<Map<String, Object>> model) throws ServiceException {
        Map<String, Object> result = new HashMap<>(16);
        try {
            insetMenuMemory(model.getModel());
            processModel(model);
            setParamsModel(model.getModel());
            int count = baseService.selectCount(model.getModel());
            List<TblCostWeightRecord> rows = baseService.selectByPage(new SinglePage(model), model.getModel(),
                    model.getOrderByField(), model.getOrderBy());
            formatData(rows);
            result.put("rows", rows);
            result.put("total", count);
            setFootModel(result, model.getModel());
        } catch (Exception ex) {
            throw new ServiceException("获取列表出错", ex);
        }
        return new ObjectDataResponse<Map>(result, 200, "获取列表成功");
    }

}

